package edu.zut.main.thread;

public class TicketTest {

    public static void main(String[] args) {
        TicketThread ticketThread = new TicketThread();

        Thread thread = new Thread(ticketThread, "小花");
        Thread thread1 = new Thread(ticketThread, "小明");
        Thread thread2 = new Thread(ticketThread, "小李");
        thread.start();
        thread1.start();
        thread2.start();
    }
}
//利用多线程模拟多人售票
class TicketThread implements Runnable{
    SaleTicket saleTicket = new SaleTicket();
    //售票任务
    @Override
    public void run() {
        while (true) {
            if (saleTicket.getTicketNum() > 0)
                saleTicket.sale();
            else
                break;
        }
    }
}

//售票操作
class SaleTicket{
    private int ticketNum = 10;

    public int getTicketNum() {
        return ticketNum;
    }

    public void sale(){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "售票" + ticketNum--);

    }

}






